<?php
namespace Home\Controller;
use Think\Controller;
class XuqiuController extends Controller {
	//需求发布
    public function index()
    {
        $this->display();
    }

    //需求入库
    public function xuqiu_add()
    {//Array ( [name] => x [budget] => x [phone] => x [num] => x [addr] => x [qq] => x [content] => x )
    	if (IS_POST) {
            $data = I('post.');
            $data['add_time'] = time();

            //验证手机
             $preg_js = "/^1[3458][0-9]{9}$/";
             $pass_sj = preg_match($preg_js, $data['phone']);
             if (!$pass_sj) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的手机号'
                    );
                echo json_encode($arr);
                exit;
             }

            //验证预算
             if (!is_numeric($data['budget'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的预算'
                    );
                echo json_encode($arr);
                exit;
             }

            //验证数量
             if (!is_numeric($data['num'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的数量'
                    );
                echo json_encode($arr);
                exit;
             }

            //验证QQ号
             $preg_qq = "/^[1-9]\d{4,10}$/";
             $check_qq = preg_match($preg_qq, $data['qq']);
             if (!$check_qq) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入正确的QQ号'
                    );
                echo json_encode($arr);
                exit;
             }

            //需求描述 不能为空
             if (empty($data['content'])) {
                $arr = array(
                    'error' => 0,
                    'info' => '请输入需求描述'
                    );
                echo json_encode($arr);
                exit;
             }

            //入库
             $res = M('xuqiu')->add($data);
             if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '欢迎您的预定,请保持手机畅通等待工作人员的联系'
                    );
                echo json_encode($arr);
             }else {
                $arr = array(
                    'error' => 0,
                    'info' => '预定失败,请重新提交'
                    );
                echo json_encode($arr);
             }

        }
    }





}